package org.example.handler;


import lombok.extern.slf4j.Slf4j;
import org.example.common.Result;
import org.example.exception.ServiceException;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 捕获业务异常
     *
     * @param ex
     * @return
     */
    //捕获不了自定义异常
    @ExceptionHandler(ServiceException.class)
    @Order(-1)
    public Result exceptionHandler(ServiceException ex) {
        log.error("异常信息：{}", ex.getMessage());
        return Result.error(ex.getCode(), ex.getMessage());
    }

    /**
     * 捕获其他系统异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @Order(3)
    public Result exceptionHandler(Exception ex) {
        ex.printStackTrace();
        log.error("异常信息：{}", ex.getMessage());
        return Result.error("系统异常, 请联系管理员");
    }

}
